package com.example.yskjproject.utils;

import android.content.Context;

import com.tencent.mmkv.MMKV;

import java.util.Objects;

/**
 * SpUtils  缓存
 * <p>
 * 作者：zx
 * 创建时间：2024/4/19
 */
public class SpManager {

    synchronized public static void put(String key, String value) {
        //        sp.edit().putString(key, value).commit();
        if (Objects.equals(getString(key),value)){
            return;
        }
        MMKV.defaultMMKV().encode(key, value);
    }

    synchronized public static void put(String key, boolean value) {
        //        sp.edit().putBoolean(key, value).commit();
        if (Objects.equals(getBoolean(key),value)){
            return;
        }
        MMKV.defaultMMKV().encode(key, value);
    }

    synchronized public static void put(String key, int value) {
        //        sp.edit().putInt(key, value).commit();
        if (Objects.equals(getInt(key),value)){
            return;
        }
        MMKV.defaultMMKV().encode(key, value);
    }

    synchronized public static void put(String key, long value) {
        //        sp.edit().putLong(key, value).commit();
        if (Objects.equals(getLong(key),value)){
            return;
        }
        MMKV.defaultMMKV().encode(key, value);
    }

    synchronized public static void put(String key, float value) {
        //        sp.edit().putFloat(key, value).commit();
        if (Objects.equals(getFloat(key),value)){
            return;
        }
        MMKV.defaultMMKV().encode(key, value);
    }

    synchronized public static boolean remove(String key) {
        if (MMKV.defaultMMKV().contains(key)) {
            MMKV.defaultMMKV().remove(key);
            return true;
        } else {
            return false;
        }
        //        if (sp.contains(key)){
        //            sp.edit().remove(key).commit();
        //            return true;
        //        }else{
        //            return false;
        //        }
    }

    public static boolean contains(String key) {
        //        return sp.contains(key);
        return MMKV.defaultMMKV().contains(key);
    }

    public static String get(String key) {
        //        return sp.getString(key, null);
        return MMKV.defaultMMKV().getString(key, "");
    }

    public static boolean getBoolean(String key) {
        //        return sp.getBoolean(key, false);
        return MMKV.defaultMMKV().getBoolean(key, false);
    }

    public static boolean getBoolean(String key, boolean _default) {
        //        return sp.getBoolean(key, _default);
        return MMKV.defaultMMKV().getBoolean(key, _default);
    }

    public static String getString(String key) {
        //        return sp.getString(key, "");
        return MMKV.defaultMMKV().getString(key, "");
    }

    public static String getString(String key, String defultValue) {
        //        return sp.getString(key, defultValue);
        return MMKV.defaultMMKV().getString(key, defultValue);
    }

    public static int getInt(String key) {
        //        return sp.getInt(key, DEAFULT);
        return MMKV.defaultMMKV().getInt(key, 0);
    }

    public static int getInt(String key, int default_int) {
        //        return sp.getInt(key, default_int);
        return MMKV.defaultMMKV().getInt(key, default_int);
    }

    public static long getLong(String key) {
        //        return sp.getLong(key, DEAFULT);
        return MMKV.defaultMMKV().getLong(key, 0L);
    }

    public static float getFloat(String key, float default_float) {
        //        return sp.getFloat(key, default_float);
        return MMKV.defaultMMKV().getFloat(key, default_float);
    }

    public static float getFloat(String key) {
        //        return sp.getFloat(key, DEAFULT);
        return MMKV.defaultMMKV().getFloat(key, 0f);
    }

}
